<?php 
class ShopShipping
{
	public $Commander;
	
	public function ShopShipping($commander)
	{ $this->Commander = $commander; }
	
	function addShopShipping($title)
	{
		if($title != '')
		{
			$insert  = 'INSERT INTO '.DB_PERFIX.'shop_shipping (`title`)'; 
			$insert .= 'VALUES ("'.$title.'");';
			$result = mysql_query($insert);
			if ($result > 0)
			{
				$result = mysql_query("SELECT id, title FROM ".DB_PERFIX."shop_shipping WHERE title LIKE '".$title."' ORDER BY id DESC LIMIT 1;");
				while($row = mysql_fetch_array($result))
				{
					$item['id'] 			= $row['id'];
					$item['title'] 			= $row['title'];
				}
				return $item;
			}
		}
	}
	
	function saveShopShipping($id, $title, $FreeShippingDateActiveStart='', $FreeShippingDateActiveEnd='')
	{	
		if($id > 0)
		{
			$title = $this->Commander->formatText($title);
			$update  = 'UPDATE '.DB_PERFIX.'shop_shipping SET ';
			$update .= 'title="'.$title.'" ';
			if(strtotime($FreeShippingDateActiveStart) > 0)
			{ $update .= ', FreeShippingDateActiveStart="'.date('Y-m-d h:i:s', strtotime($FreeShippingDateActiveStart)).'" '; }
			if(strtotime($FreeShippingDateActiveEnd) > 0) 
			{ $update .= ', FreeShippingDateActiveEnd="'.date('Y-m-d h:i:s', strtotime($FreeShippingDateActiveEnd)).'" '; }
			$update .= 'WHERE id='.$id.' LIMIT 1;';
			$update = mysql_query($update);
		}
	}
	
	function deleteShopShipping($id)
	{	
		if($id > 0)
		{	
			$delete = 'DELETE FROM '.DB_PERFIX.'shop_shipping WHERE id='.$id.' LIMIT 1;';
			$result = mysql_query($delete);
			return ;
		}
	}
	
	function getShopShippingMethods($id=0)
	{
		if($id > 0)
		{ $select = 'SELECT * FROM '.DB_PERFIX.'shop_shipping WHERE id='.$id.';'; }
		else
		{ $select = 'SELECT * FROM '.DB_PERFIX.'shop_shipping WHERE (site_id=0 OR site_id='.SITE_ID.');'; }
		$result = mysql_query($select);
		if(mysql_num_rows($result) > 0)
		{
			while($row = mysql_fetch_array($result))
			{
				foreach(array_keys($row) as $key)
				{ $item[$row['id']][$key] = $row[$key]; }
			}
			return $item; 
		}
	}
	
	function addShopShippingZone($title, $parent, $country='')
	{
		if(($title != '') && ($parent > 0))
		{
			$insert  = ' INSERT INTO '.DB_PERFIX.'shop_shipping_zones (`parent_id`, `title`, `country`) '; 
			$insert .= ' VALUES ('.$parent.', "'.$title.'", "'.$country.'");';
			$result = mysql_query($insert);
			if ($result > 0)
			{
				$result = mysql_query("SELECT id, title FROM ".DB_PERFIX."shop_shipping WHERE title LIKE '".$title."' ORDER BY id DESC LIMIT 1;");
				while($row = mysql_fetch_array($result))
				{
					$item['id'] 			= $row['id'];
					$item['title'] 			= $row['title'];
				}
				return $item;
			}
		}
	}
	
	function saveShopShippingZone($id, $title, $country, $code='', $email='')
	{	
		if($id > 0)
		{
			$update  = 'UPDATE '.DB_PERFIX.'shop_shipping_zones SET ';
			$update .= 'country="'.$country.'", ';
			$update .= 'code="'.$code.'", ';
			$update .= 'email="'.$email.'", ';
			$update .= 'title="'.$title.'" ';
			$update .= 'WHERE id='.$id.' LIMIT 1;';
			$update = mysql_query($update);
		}
	}
	
	function deleteShopShippingZone($id)
	{	
		if($id > 0)
		{	
			$delete = 'DELETE FROM '.DB_PERFIX.'shop_shipping_zones WHERE id='.$id.' LIMIT 1;';
			$result = mysql_query($delete);
			return ;
		}
	}
	
	function getShopShippingZones($parent_id)
	{
		if($parent_id > 0)
		{ 
			$select = 'SELECT id, parent_id, title, country, code, email FROM '.DB_PERFIX.'shop_shipping_zones WHERE parent_id='.$parent_id.' ORDER BY id ASC;'; 
			$result = mysql_query($select);
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{
					foreach(array_keys($row) as $key)
					{ $item[$row['id']][$key] = $row[$key]; }
				}
				return $item;
			}
		}
	}
	
	function addShopShippingArea($zone_id, $city, $zip_start, $zip_end)
	{
		if(($zone_id > 0) && (($zip_start > 0) || ($zip_end > 0)))
		{
			$insert  = ' INSERT INTO '.DB_PERFIX.'shop_shipping_areas (`zone_id`, `city`, `zip_start`, `zip_end`) '; 
			$insert .= ' VALUES ('.$zone_id.', "'.$city.'", '.$zip_start.', '.$zip_end.');';
			$result = mysql_query($insert);
			return $result;
		}
	}
	
	function saveShopShippingArea($id, $city, $zip_start, $zip_end)
	{	
		if($id > 0)
		{
			$update  = 'UPDATE '.DB_PERFIX.'shop_shipping_areas SET ';
			$update .= 'city="'.$city.'", ';
			$update .= 'zip_start='.$zip_start.', ';
			$update .= 'zip_end='.$zip_end.' ';
			$update .= 'WHERE id='.$id.' LIMIT 1;';
			$update = mysql_query($update);
		}
	}
	
	function deleteShopShippingArea($id)
	{	
		if($id > 0)
		{	
			$delete = 'DELETE FROM '.DB_PERFIX.'shop_shipping_areas WHERE id='.$id.' LIMIT 1;';
			$result = mysql_query($delete);
			return ;
		}
	}
	
	function getShopShippingAreas($zone_id=0)
	{
		if($zone_id > 0)
		{ $select = 'SELECT id, zone_id, city, zip_start, zip_end FROM '.DB_PERFIX.'shop_shipping_areas WHERE zone_id='.$zone_id.';'; }
		else
		{ $select = 'SELECT id, zone_id, city, zip_start, zip_end FROM '.DB_PERFIX.'shop_shipping_areas;'; }
		
		$result = mysql_query($select);
		if(mysql_num_rows($result) > 0)
		{
			while($row = mysql_fetch_array($result))
			{
				foreach(array_keys($row) as $key)
				{ $item[$row['zone_id']][$row['id']][$key] = $row[$key]; }
			}
			return $item;
		}
	}
	
	function addShopShippingRule($shipping_id, $zone_id, $title='', $min_weight=0, $max_weight=0, $min_volume=0, $max_volume=0, $price=0, $priceSat=0, $priceSun=0)
	{
		if(($shipping_id > 0) && (is_numeric($zone_id)))
		{
			if(!(is_numeric($priceSat)))
			{ $priceSat = 0; }
			if(!(is_numeric($priceSun)))
			{ $priceSun = 0; }
			$title = trim(htmlspecialchars($title));
			$insert  = ' INSERT INTO '.DB_PERFIX.'shop_shipping_rules (`shipping_id`, `zone_id`, `title`, `min_weight`, `max_weight`, `min_volume`, `max_volume`, `price`, `price_delivery_saturday`, `price_delivery_sunday`) '; 
			$insert .= ' VALUES ('.$shipping_id.', '.$zone_id.', "'.$title.'", '.$min_weight.', '.$max_weight.', '.$min_volume.', '.$max_volume.', '.$price.', '.$priceSat.', '.$priceSun.');';
			$result = mysql_query($insert);
			if($zone_id > 0)
			{
				$rules = self::getShopShippingRules($shipping_id, 0, $min_weight, $max_weight, $min_volume);
				if(count($rules) == 1)
				{
					$zones = self::getShopShippingZones($shipping_id);
					if($zones)
					{
						foreach($zones as $zone)
						{
							if($zone['id'] != $zone_id)
							{
								$insert  = ' INSERT INTO '.DB_PERFIX.'shop_shipping_rules (`shipping_id`, `zone_id`, `title`, `min_weight`, `max_weight`, `min_volume`, `max_volume`, `price`) '; 
								$insert .= ' VALUES ('.$shipping_id.', '.$zone['id'].', "'.$title.'", '.$min_weight.', '.$max_weight.', '.$min_volume.', '.$max_volume.', '.$price.');';
								$result = mysql_query($insert);
							}
						}
					}
				}
			}
			return $result;
		}
	}
	
	function saveShopShippingRule($id, $title, $zone_id, $min_weight=0, $max_weight=0, $min_volume=0, $max_volume=0, $price=0, $priceSat=0, $priceSun=0)
	{	
		if($id > 0)
		{
			$title = trim(htmlspecialchars($title));
			
			$update  = 'UPDATE '.DB_PERFIX.'shop_shipping_rules SET ';
			$update .= 'title="'.$title.'", ';
			$update .= 'zone_id='.$zone_id.', ';
			$update .= 'min_weight='.$min_weight.', ';
			$update .= 'max_weight='.$max_weight.', ';
			$update .= 'min_volume='.$min_volume.', ';
			$update .= 'max_volume='.$max_volume.', ';
			if(is_numeric($priceSat))
			{ $update .= 'price_delivery_saturday='.$priceSat.', '; }
			if(is_numeric($priceSun))
			{ $update .= 'price_delivery_sunday='.$priceSun.', '; }
			$update .= 'price='.$price.' ';
			$update .= 'WHERE id='.$id.' LIMIT 1;';
			//echo $update;
			$update = mysql_query($update);
		}
	}
	
	function deleteShopShippingRule($id)
	{	
		if($id > 0)
		{	
			$delete = 'DELETE FROM '.DB_PERFIX.'shop_shipping_rules WHERE id='.$id.' LIMIT 1;';
			$result = mysql_query($delete);
			return ;
		}
	}
	
	function getShopShippingRules($shipping, $zone=0, $min_weight=0, $max_weight=0, $min_volume=0, $max_volume=0)
	{
		if($shipping > 0)
		{ 
			$where = '';
			if($zone > 0)
			{ $where .= ' AND zone_id='.$zone; }
			if($min_weight > 0)
			{ $where .= ' AND min_weight='.$min_weight; }
			if($max_weight > 0)
			{ $where .= ' AND max_weight='.$max_weight; }
			if($min_volume > 0)
			{ $where .= ' AND min_volume='.$min_volume; }
			if($max_volume > 0)
			{ $where .= ' AND max_volume='.$max_volume; }
			
			$select = 'SELECT id, shipping_id, zone_id, title, min_weight, max_weight, min_volume, max_volume, price, price_delivery_saturday, price_delivery_sunday FROM '.DB_PERFIX.'shop_shipping_rules WHERE shipping_id='.$shipping.$where.' ORDER BY id ASC;'; 
			$result = mysql_query($select);
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{
					foreach(array_keys($row) as $key)
					{ $item[$row['id']][$key] = $row[$key]; }
				}
				return $item;
			}
		}
	}
	
	function getShopShippingMethodsMenuHTML($methods)
	{
		if($methods)
		{
			$output .= '<ul>';
			foreach($methods as $method)
			{
				$style = ' style="background-image:url('.SYSICONS_URL.'treemenu/shipping_16.gif)" ';
				$output .= '<li id="shipping_'.$method['id'].'" class="'.$li_class.' leaf" '.$state.' rel="shipping_method">';
				$output .= '<a '.$style.' href="?con='.$_REQUEST['con'].'&amp;shipping='.$method['id'].'">';
				$output .= $method['title'];
				$output .= '</a>';
				$output .= '</li>';	
			}
			$output .= '</ul>';
		}
		return $output;
	}
}
?>